package com.rich.dmr.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.rich.dmr.model.ChannelModel;
import com.rich.dmr.model.Frequency;
import com.rich.dmr.model.GroupModel;
import com.rich.dmr.model.RxGroupModel;

import java.util.ArrayList;
import java.util.List;

public class DataBaseUtils {
    private String TAG = "rich---DataBaseUtils";
    private static DataBaseUtils mDataBaseUtils;
    private ContentResolver mContentResolver;
    Uri channelUri = Uri.parse("content://com.provider.dmr/channel_list");
    Uri rxListUri = Uri.parse("content://com.provider.dmr/rx_list");
    Uri groupContactUri = Uri.parse("content://com.provider.dmr/dmr_contact");
    public static DataBaseUtils getInstance(Context context) {
        if (mDataBaseUtils == null) {
            synchronized (DataBaseUtils.class) {
                mDataBaseUtils = new DataBaseUtils(context);
            }
        }
        return mDataBaseUtils;
    }
    private DataBaseUtils(Context context) {
        mContentResolver = context.getContentResolver();
    }
    public boolean insertChannel(String channelName, int txFre, int rxFre) {
        ContentValues values = new ContentValues();
        values.put("channel_name", channelName);
        values.put("tx_fre", txFre);
        values.put("rx_fre", rxFre);
        Uri uri = mContentResolver.insert(channelUri, values);
        assert uri != null;
        String url = uri.toString();
        String insertUrl = url.substring(url.length() - 2);
        return insertUrl.equals("ok");
    }

    public boolean insertRxList(String rxName, String rxList) {
        ContentValues values = new ContentValues();
        values.put("rx_name", rxName);
        values.put("rx_list", rxList);
        Uri uri = mContentResolver.insert(rxListUri, values);
        assert uri != null;
        String url = uri.toString();
        String insertUrl = url.substring(url.length() - 2);
        return insertUrl.equals("ok");
    }

    public boolean insertGroupContact(String groupName, int groupNum) {
        ContentValues values = new ContentValues();
        values.put("name", groupName);
        values.put("phone", groupNum);
        values.put("call_type", 1);
        values.put("color", ConvertUtils.getRandColorCode());
        Uri uri = mContentResolver.insert(groupContactUri, values);
        assert uri != null;
        String url = uri.toString();
        String insertUrl = url.substring(url.length() - 2);
        return insertUrl.equals("ok");
    }

    public List<ChannelModel> findChannelList() {
        List<ChannelModel> list = new ArrayList<>();
        Cursor cursor = mContentResolver.query(channelUri, null, null, null, "channel_name asc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String channelName = cursor.getString(cursor.getColumnIndex("channel_name"));
                int txFre= cursor.getInt(cursor.getColumnIndex("tx_fre"));
                int rxFre = cursor.getInt(cursor.getColumnIndex("rx_fre"));
                list.add(new ChannelModel(id,channelName, txFre,rxFre));
            }
            cursor.close();
        }
        return list;
    }

    public List<RxGroupModel> findRxGroupList() {
        List<RxGroupModel> list = new ArrayList<>();
        Cursor cursor = mContentResolver.query(rxListUri, null, null, null, "rx_name asc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String rxGroupName = cursor.getString(cursor.getColumnIndex("rx_name"));
                String rxList= cursor.getString(cursor.getColumnIndex("rx_list"));
                list.add(new RxGroupModel(id,rxGroupName, rxList));
            }
            cursor.close();
        }
        return list;
    }

    public List<GroupModel> findGroupContact() {
        List<GroupModel> list = new ArrayList<>();
        Cursor cursor = mContentResolver.query(groupContactUri, null, "call_type=?", new String[]{"1"}, "name asc");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String groupContactName = cursor.getString(cursor.getColumnIndex("name"));
                int groupNum= cursor.getInt(cursor.getColumnIndex("phone"));

                list.add(new GroupModel(id,groupContactName, groupNum));
            }
            cursor.close();
        }
        return list;
    }

    public int findGroupContact(int group) {
        Cursor cursor = mContentResolver.query(groupContactUri, null, "phone=? and call_type=?", new String[]{String.valueOf(group),"1"}, null);
        int count = 0;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                count = cursor.getCount();
                Log.d(TAG, "count =" + count);
            }
            cursor.close();
        }
        return count;
    }

    public int findRxGroup(String groupList) {
        Cursor cursor = mContentResolver.query(rxListUri, null, "rx_list=?", new String[]{groupList}, null);
        int count = 0;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                count = cursor.getCount();
                Log.d(TAG, "count =" + count);
            }
            cursor.close();
        }
        return count;
    }

    public int findChannel(int txChannel,int rxChannel) {
        Cursor cursor = mContentResolver.query(channelUri, null, "tx_fre=? and rx_fre=?", new String[]{String.valueOf(txChannel),String.valueOf(rxChannel)}, null);
        int count = 0;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                count = cursor.getCount();
                Log.d(TAG, "count =" + count);
            }
            cursor.close();
        }
        return count;
    }

    public boolean deleteChannelById(int id){
        int res = mContentResolver.delete(channelUri,"_id=?",new String[]{String.valueOf(id)});
        return res == 0;
    }

    public boolean deleteContactById(int id){
        int res = mContentResolver.delete(groupContactUri,"_id=?",new String[]{String.valueOf(id)});
        return res == 0;
    }

    public boolean deleteRxListById(int id){
        int res = mContentResolver.delete(rxListUri,"_id=?",new String[]{String.valueOf(id)});
        return res == 0;
    }
}
